const App = getApp()
import $CountDown from '../../utils/countdown.js'
import { SeckillModel } from '../../models/seckill'
const ModelSeckill = new SeckillModel()

Page({
  data: {
    statusBarHeight: App.globalData.statusBarHeight, //顶部导航栏高度
    isTabbar: false, //是不是主页面
    iphoneX: App.globalData.isIphoneX,
    duration: 500,
    currentTab: 0,
    navs: [],
    goodsList: [],
    hours: 0,
    min: 0,
    sec: 0,
    placeholder: {
      status: 0,
      showTime: '敬请期待'
    },
    clientWidth: '',
    winHeight: '',
    seckillId: '', //秒杀id
    seckillTime: '', //秒杀时间
    secondNavs: []
  },
  onLoad(options) {
    console.log('options == ', options)
    // Do some initialize when page load.
    const that = this

    //获取秒杀商品列表
    that.selectActivitySeckill(options.seckillId)
  },
  //nav切换
  selectItme: function(e) {
    const that = this
    const index = e.currentTarget.dataset.index
    const navs = that.data.navs
    const secondNavs = that.data.navs
    //如果选中的是自己则返回
    if (navs[index].active == true) {
      return
    }
    //如果选中的是已结束或者敬请期待则什么都不做
    if (navs[index].status == 0 || navs[index].status == 3) {
      return
    }
    // for(var i in navs){
    //   navs[i].active = false;
    //   secondNavs[i].active = false;
    // }
    // navs[index].active = true;
    // that.setData({
    //   currentTab:index,
    //   navs:navs,
    //   secondNavs:secondNavs
    // })
    that.selectChange(navs[index].id, index)
  },
  //商品详情
  tapToDetail(e) {
    const that = this
    const percent = e.currentTarget.dataset.percent
    const id = e.currentTarget.dataset.id
    const navs = that.data.navs
    if (navs[that.data.currentTab].status != 3 && percent < 100) {
      wx.navigateTo({
        url: `/pages/goodsDetail/index?id=${id}`
      })
    }
  },
  //首次进入获取活动秒杀商品
  selectActivitySeckill(id, reload) {
    const that = this
    let seckillTime = (that.data.seckillTime = '')
    that.setData({
      seckillId: id
    })
    const params = {
      seckillId: id
    }
    ModelSeckill.selectActivitySeckill(params)
      .then(res => {
        let navs = that.data.navs
        let secondNavs = that.data.navs
        that.setData({
          navs: [],
          secondNavs: []
        })
        let currentTab = that.data.currentTab
        let arrs = []
        //判断是否有活动中的
        navs = res.data.seckillTitile
        for (var i in navs) {
          navs[i].active = false
          //查询已结束的数量
          if (navs[i].status == 3) {
            arrs.push(navs[i])
          }
          //跳转对应
          if (navs[i].status == 2 && navs[i].id == id) {
            currentTab = i
            navs[i].active = true
          }
        }
        //如果没有活动中的，则给第一个即将开始
        if (!navs[currentTab].active) {
          for (var i in navs) {
            if (navs[i].status != 3) {
              navs[i].active = true
              currentTab = i
              break
            }
          }
        }
        if (arrs.length == res.data.seckillTitile.length) {
          wx.showModal({
            title: '提示',
            content: '秒杀已结束，点击返回首页',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.reLaunch({
                  url: '/pages/home/index'
                })
              }
            }
          })
          return
        }
        //即将开始
        if (navs[currentTab].status == 1) {
          seckillTime = navs[currentTab].startDate
        }
        //活动中
        if (navs[currentTab].status == 2) {
          seckillTime = navs[currentTab].endDate
        }
        that.setData({
          seckillTime: seckillTime,
          navs: navs,
          currentTab: currentTab,
          secondNavs: navs,
          goodsList: res.data.seckillGoods
        })
        //获取屏幕宽度
        that.getClientWidth()
      })
      .catch()
  },
  //点击获取详情
  selectChange(id, index) {
    const that = this
    let seckillTime = that.data.seckillTime
    that.setData({
      seckillId: id
    })
    const params = {
      seckillId: id
    }
    ModelSeckill.selectActivitySeckill(params)
      .then(res => {
        that.setData({
          navs: []
        })
        let navs = res.data.seckillTitile
        let secondNavs = navs

        let arrs = []
        //判断是否有活动中的
        // navs[currentTab].active = true
        //即将开始
        if (secondNavs[index].status == 1) {
          seckillTime = secondNavs[index].startDate
        }
        //活动中
        if (secondNavs[index].status == 2) {
          seckillTime = secondNavs[index].endDate
        }
        for (var i in secondNavs) {
          secondNavs[i].active = false
        }
        secondNavs[index].active = true
        that.setData({
          seckillTime: seckillTime,
          navs: secondNavs,
          currentTab: index,
          goodsList: res.data.seckillGoods
        })
        //获取屏幕宽度
        that.getClientWidth()
      })
      .catch()
  },
  //获取倒计时
  getCountTime(time, id) {
    const that = this
    that.setData({
      hours: 0,
      min: 0,
      sec: 0
    })
    new $CountDown({
      date: new Date(time),
      render(date) {
        const hours = this.leadingZeros(date.hours, 2)
        const min = this.leadingZeros(date.min, 2)
        const sec = this.leadingZeros(date.sec, 2)
        if (hours == 0 && min == 0 && sec == 0) {
          that.selectActivitySeckill(id, true)
        }
        that.setData({
          hours: hours,
          min: min,
          sec: sec
        })
      }
    })
  },
  //获取系统宽度
  getClientWidth() {
    const that = this
    let statusBarHeight = that.data.statusBarHeight
    wx.getSystemInfo({
      success(res) {
        let clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth
        // var calc = clientHeight * rpxR - 200
        var calc = clientHeight - 44 - statusBarHeight - 46
        that.setData({
          clientWidth: res.windowWidth - 24,
          winHeight: calc
        })
      }
    })
  },
  //滑动
  switchTab: function(e) {
    const that = this
    let navs = that.data.navs
    let index = e.detail.current
    if (e.detail.source == 'touch') {
      //如果选中的是自己则返回
      if (navs[index].active == true) {
        return
      }
      //如果选中的是已结束或者敬请期待则什么都不做
      if (navs[index].status == 0 || navs[index].status == 3) {
        return
      }
      for (var i in navs) {
        navs[i].active = false
      }
      navs[index].active = true
      that.setData({
        currentTab: index,
        navs: navs
      })
      that.selectActivitySeckill(navs[that.data.currentTab].id, false)
    }
  },
  //设置开抢提醒
  setup(e) {
    const that = this
    const seckillGoodsId = e.currentTarget.dataset.id
    const seckillId = that.data.seckillId
    const idx = e.currentTarget.dataset.idx
    let goodsList = that.data.goodsList
    const params = {
      seckillId: seckillId,
      seckillGoodsId: seckillGoodsId
    }

    ModelSeckill.makeSeckillGoods(params)
      .then(res => {
        goodsList[idx].makeStatus = 1
        that.setData({
          goodsList: goodsList
        })
        wx.showToast({
          title: '提醒已设置，将在开抢前十分钟提醒你',
          duration: 1000,
          icon: 'none',
          success: function() {
            // setTimeout( () =>{
            //   that.selectActivitySeckill(seckillId,false);
            // },2000)
          }
        })
      })
      .catch()
  },
  //倒计时截止
  deadline(id) {
    that.selectActivitySeckill(navs[that.data.currentTab].id, true)
  }
})
